<template>
  <div>
    <el-form
      ref="query"
      :model="query"
      :inline="true"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="图书编号">
        <el-input v-model="query.book_num" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="query.book_name" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="query.writer" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="出版社">
        <el-input v-model="query.publisher" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="所在位置">
        <el-input v-model="query.site" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="可否借阅">
        <el-input v-model="query.isBorrow" style="width: 120px"></el-input>
      </el-form-item>
    </el-form>

    <div class="btn">
      <div class="left">
        <el-button
          class="el-icon-plus"
          type="primary"
          size="mini"
          style="text-align: left"
          @click="addBook"
          >图书入库</el-button
        >
      </div>
      <div class="right">
        <el-button
          @click="bookQuery"
          style="text-align: right"
          size="mini"
          type="primary"
          icon="el-icon-search"
          >查询</el-button
        >
        <el-button
          @click="resetForm"
          style="text-align: right"
          size="mini"
          icon="el-icon-refresh"
          >重置</el-button
        >
      </div>
    </div>

    <el-table :data="tableData" border size="mini">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="book_num" label="图书编号" width="180">
      </el-table-column>
      <el-table-column prop="book_name" label="图书名称" width="180">
      </el-table-column>
      <el-table-column prop="writer" label="作者" width="90"> </el-table-column>
      <el-table-column prop="publisher" label="出版社" width="150">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="90"> </el-table-column>
      <el-table-column prop="site" label="所在位置" width="90">
      </el-table-column>
      <el-table-column prop="amount" label="图书数量" width="70">
      </el-table-column>
      <!-- <el-table-column label="可否借阅" width="95">
        <template v-slot="scope">
          <el-tag size="medium" type="success" v-if="scope.row.amount > 0">{{
            "可借阅"
          }}</el-tag>
          <el-tag size="medium" type="danger" v-if="scope.row.amount == 0">{{
            "不可借阅"
          }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column type="expand" label="图书简介" width="100">
        <template v-slot="props">
          <el-form label-position="right" inline class="demo-table-expand">
            <el-form-item>
              <span>{{ props.row.introduction }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <!-- <el-table-column type="expand" label="查看借阅者" width="100">
        <template v-slot="props">
          <el-form label-position="right" inline class="demo-table-expand">
            <el-form-item>
              <span>{{ props.row.introduction }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="250">
        <template v-slot="scope">
          <el-button
            size="mini"
            icon="el-icon-edit"
            type="text"
            @click="editBook(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            icon="el-icon-delete"
            type="text"
            @click="deleteBook(scope.$index, scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="findUser(scope.$index, scope.row)"
            >查看借阅者</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" v-model="editDialogShow" width="30%">
      <el-form
        ref="bookFormRules"
        :rules="bookFormRules"
        :model="bookForm"
        :inline="true"
        label-width="80px"
        size="mini"
      >
        <el-form-item label="图书编号">
          <el-input v-model="bookForm.book_num"></el-input>
        </el-form-item>
        <el-form-item label="图书名称">
          <el-input v-model="bookForm.book_name"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="bookForm.writer"></el-input>
        </el-form-item>
        <el-form-item label="出版社">
          <el-input v-model="bookForm.publisher"></el-input>
        </el-form-item>
        <el-form-item label="图书简介">
          <el-input v-model="bookForm.introduction"></el-input>
        </el-form-item>
        <el-form-item label="所在位置">
          <el-input v-model="bookForm.site"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="bookForm.price"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="bookForm.amount"></el-input>
        </el-form-item>
        <el-form-item style="text-align: right">
          <el-button type="primary" @click="onSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableDataTotal"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  page,
  create,
  update,
  bookDelete,
} from "../../api/book/book";
export default {
  data() {
    return {
      tableData: [],
      query: { page: 1, limit: 10 },
      title: "图书入库",
      isAdd: true,
      editDialogShow: false,
      bookFormRules: {
        book_num: [
          { required: true, message: "请输入图书编号", trigger: "blur" },
        ],
        book_name: [
          { required: true, message: "请输入图书名称", trigger: "blur" },
        ],
        writer: [{ required: true, message: "请输入作者", trigger: "blur" }],
        publisher: [
          { required: true, message: "请输入出版社", trigger: "blur" },
        ],
        introduction: [
          { required: true, message: "请输入图书简介", trigger: "blur" },
        ],
        site: [{ required: true, message: "请输入图书位置", trigger: "blur" }],
        price: [{ required: true, message: "请输入图书价格", trigger: "blur" }],
        amount: [
          { required: true, message: "请输入图书数量", trigger: "blur" },
        ],
      },
      bookForm: {},
      tableDataTotal: 0,
    };
  },
  created() {
    this.getPage();
    // console.log($store.state.user);
  },
  methods: {
    indexMethod(index) {
      return (this.query.page - 1) * this.query.limit + (index + 1);
    },
    findUser(index,row) {
        this.$router.push({
          path:'/book/userInfo',
          query:{book_num:row.book_num}
        });
    },
    getPage() {
      page(this.query).then((res) => {
        console.log(res);
        // console.log(this.$store.state.user);
        this.tableData = res.data.rows;
        this.tableDataTotal = res.data.count;
      });
    },
    handleSizeChange(val) {
      this.query.limit = val;
      this.getPage();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getPage();
    },
    bookQuery() {
      this.getPage();
    },
    resetForm() {
      this.query = { page: 1, limit: 10 };
      this.getPage();
    },
    addBook() {
      this.isAdd = true;
      this.editDialogShow = true;
      this.bookForm = {};
      this.title = "图书入库";
    },
    editBook(index, row) {
      this.isAdd = false;
      this.editDialogShow = true;
      this.bookForm = { ...row };
      this.title = "编辑图书";
    },
    onSubmit() {
      this.$refs.bookFormRules.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.create();
          } else {
            this.update();
          }
        } else {
          return false;
        }
      });
    },
    create() {
      this.bookForm.price = parseInt(this.bookForm.price);
      this.bookForm.amount = parseInt(this.bookForm.amount);
      create(this.bookForm)
        .then((res) => {
          if (res.success) {
            this.$message({ message: "图书入库成功！", type: "success" });
            this.editDialogShow = false;
            this.getPage();
          } else {
            this.$message.error(res.data);
          }
        })
        .catch(() => this.$message.error("图书入库失败！"));
    },
    update() {
      this.bookForm.price = parseInt(this.bookForm.price);
      this.bookForm.amount = parseInt(this.bookForm.amount);
      update(this.bookForm)
        .then((res) => {
          if (res.success) {
            this.$message({ message: "图书修改成功！", type: "success" });
            this.editDialogShow = false;
            this.getPage();
          } else {
            this.$message.error(res.data);
          }
        })
        .catch(() => this.$message.error("图书修改失败！"));
    },
    deleteBook(index, row) {
      this.$confirm(
        `此操作将删除【${row.book_name}】这本书, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          bookDelete({ book_num: row.book_num }).then((res) => {
            if (res.success) {
              this.$message({ type: "success", message: "删除成功!" });
              this.getPage();
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.btn {
  height: 35px;
  .left {
    float: left;
  }
  .right {
    float: right;
    padding-right: 62px;
  }
}
</style>